<?php

namespace app\api\controller;

use app\api\model\CardOrder as CardOrderModel;
use app\api\service\Payment as PaymentService;
use app\common\enum\order\PayType as PayTypeEnum;
use app\common\enum\OrderType as OrderTypeEnum;
use think\Request;
use app\api\model\store\shop\Clerk as ClerkModel;
class Card extends Controller
{
    public $model;
    public $user;


    public function __construct(Request $request = null)
    {
        $this->model = new CardOrderModel();
        parent::__construct($request);
        // 用户信息
        $this->user = $this->getUser();
    }
    //获取时间日期
    public function fixedTime(){
        $data = $this->model->fixedTime();
        return $this->renderSuccess($data,'获取数据');
    }
    //获取卡座列表
    public function cardList(){
        $data = ClerkModel::getCard();
        return $this->renderSuccess($data,'获取数据成功');
    }
    //创建订单
    public function createOrder(Request $request = null){
        $param = $request->param();
        $card_id = CardOrderModel::createOrder($param);
        return $this->renderSuccess($card_id,'操作成功!');
    }

    /**
     * 查询订单信息
     * @param Request|null $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetail(Request $request = null){
        $card_id = $request->param('card_id');
        $data = $this->model->getDetail($card_id,$this->user['user_id']);
        return $this->renderSuccess($data,'操作成功');
    }

    /**
     * 获取卡座订单列表
     * @param Request|null $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList(Request $request = null){
        $order_status = $request->param('order_status');
        $data = $this->model->getList($order_status,$this->user['user_id']);
        return $this->renderSuccess($data,'操作成功');
    }

    /**
     * 获取卡座订单列表
     * @param Request|null $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWrite(Request $request = null){
        $data = $this->model->getWrite($this->user['user_id']);
        return $this->renderSuccess($data,'操作成功');
    }

    /**
     * 发起支付请求
     * @param Request|null $request
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function payment(Request $request = null){
        $card_id = $request->param('card_id');
        $data = $this->model->find($card_id);
        $payment = PaymentService::wechat(
            $this->user,
            null,
            $data->order_no,
            $data->pay_amount,
            OrderTypeEnum::CARD
        );
        return $this->renderSuccess($payment,'操作成功');
    }
    /**
     * 立即支付
     * @param int $order_id 订单id
     * @param int $payType 支付方式
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function pay($card_id, $payType = PayTypeEnum::WECHAT)
    {
        // 订单详情
        $model = $this->model->getUserOrderDetail($card_id, $this->user['user_id']);
        // 订单支付事件
        if (!$model->onPay($payType)) {
            return $this->renderError($model->getError() ?: '订单支付失败');
        }
        // 构建微信支付请求
        $payment = $model->onOrderPayment($this->user, $model, $payType);
        // 支付状态提醒
        return $this->renderSuccess([
            'order_id' => $model['id'],   // 订单id
            'pay_type' => $payType,             // 支付方式
            'payment' => $payment               // 微信支付参数
        ], ['success' => '支付成功', 'error' => '订单未支付']);
    }

    /**
     * 订单核销业务逻辑
     * @param Request|null $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function clerk(Request $request = null){
        $userinfo = \app\api\model\User::find($this->user['user_id']);
        if(empty($userinfo->clerk_id)){
            return $this->renderError('您不是核销员，请联系平台');
        }
        $order_no = $request->param('order_no');
        $orderDetail = CardOrderModel::getOrderDetail($order_no);
        $orderDetail->order_status = CardOrderModel::CARD_ORDER_FINISH;
        if($orderDetail->save()){
            return $this->renderSuccess([],'订单核销成功');
        }else{
            return $this->renderError('订单核销失败');
        }
    }

    /**
     * 获取订单详情
     * @param Request|null $request
     * @return array
     * @throws \think\exception\DbException
     */
    public function getInfo(Request $request = null){
        $order_no = $request->param('order_no');
        $orderDetail = CardOrderModel::getOrderDetail($order_no);
        return $this->renderSuccess($orderDetail,'获取订单成功');
    }


}